let handler = require("./data_model_service_handler");
let customs = require("./custom/custom").data_model_service_customs;
let db = require("../db/db")

handler.set_db(db);

let handle = async function (action, data, login_info, req, res) {
	let tablename = data.tablename; //数据模型
	let result;
	switch (action) {
		case "delete":
			if (customs[tablename + "_del"]) {
				result = await customs[tablename + "_del"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.del(data, login_info, req, res);
				return result;
			}
			break;
		case "update":
			if (customs[tablename + "_update"]) {
				result = await customs[tablename + "_update"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.update(data, login_info, req, res);
				return result;
			}
			break;
		case "add":
			if (customs[tablename + "_add"]) {
				result = await customs[tablename + "_add"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.add(data, login_info, req, res);
				return result;
			}
			break;
		case "getrow":
			if (customs[tablename + "_getrow"]) {
				result = await customs[tablename + "_getrow"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.getrow(data, login_info, req, res);
				return result;
			}
			break;
		case "getrows":
			if (customs[tablename + "_getrows"]) {
				result = await customs[tablename + "_getrows"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.getrows(data, login_info, req, res);
				return result;
			}
			break;
		case "getinfo":
			if (customs[tablename + "_getinfo"]) {
				result = await customs[tablename + "_getinfo"](data, login_info, req, res);
				return result;
			}
			else {
				result = await handler.getinfo(data, login_info, req, res);
				return result;
			}
			break;
		default:
			return (Error('未知的action:' + action));
	}
}

module.exports = { handle };